<script setup lang="ts">
import { ElMessage, type FormInstance } from 'element-plus'
import { ref } from 'vue'
import type { GroupTable } from '@/types'
import { addGroup } from './api'
import { formRules } from './methodType'
const props = defineProps<{
  dMap: Map<string, string>
  // 刷新
  refresh: () => void
}>()
// 关闭
const emit = defineEmits<{
  (event: 'cDlg'): void
}>()
const addForm = ref<GroupTable>({
  name: '',
  description: ''
})
// 表单dom
const elFormRef = ref<FormInstance>()
function add() {
  elFormRef.value?.validate((d) => {
    if (d) {
      addGroup(addForm.value)
        .then((d) => {
          ElMessage.success(d.msg)
          emit('cDlg')
          props.refresh()
        })
        .catch((d) => {
          ElMessage.error(d.msg)
        })
    }
  })
}
</script>

<template>
  <el-form ref="elFormRef" :model="addForm" :rules="formRules" label-width="80px">
    <el-form-item required :label="(dMap.get('name') || 'name') + '：'" prop="name">
      <el-input
        v-model="addForm.name"
        :placeholder="'请输入' + dMap.get('name') || 'name'"
      ></el-input>
    </el-form-item>
    <el-form-item :label="(dMap.get('description') || 'description') + '：'" prop="description">
      <el-input
        v-model="addForm.description"
        :placeholder="'请输入' + dMap.get('description') || 'description'"
      ></el-input>
    </el-form-item>
    <div class="buttons">
      <el-button type="primary" @click="emit('cDlg')">取消</el-button>
      <el-button type="success" @click="add">保存</el-button>
    </div>
  </el-form>
</template>
<style scoped lang="less">
.buttons {
  display: flex;
  margin-top: 30px;
  justify-content: flex-end;
}
</style>
